#define _CRT_SECURE_NO_WARNINGS

#include "SeqList.h"

void CheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity + 8;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		assert(tmp);
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

void SLInit(SL* ps)
{
	SLDateType* tmp = (SLDateType*)malloc(sizeof(SLDateType) * INIT_CAPACITY);
	assert(tmp);
	ps->a = tmp;
	ps->size = 0;
	ps->capacity = INIT_CAPACITY;
}

void SLPrint(SL* ps)
{
	assert(ps->a);

	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SLPushBack(SL* ps, SLDateType x)
{
	CheckCapacity(ps);

	ps->a[ps->size++] = x;
}

void SLPopBack(SL* ps)
{
	assert(ps->a);

	ps->size--;
}

void SLPushFront(SL* ps, SLDateType x)
{
	CheckCapacity(ps);

	for (int i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}

void SLPopFront(SL* ps)
{
	assert(!SLEmpty(ps));

	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

bool SLEmpty(SL* ps)
{
	return ps->size == 0;
}
int SLSize(SL* ps)
{
	return ps->size;
}

void SLDestroy(SL* ps)
{
	assert(ps->a);

	free(ps->a);
	ps->capacity = ps->size = 0;
}